package com.github.nuggets.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.nuggets.constant.DataBaseConstants;
import com.github.nuggets.model.entity.NuggetsAccountEntity;
import com.github.nuggets.model.entity.NuggetsFansEntity;
import com.github.nuggets.model.result.PageResult;
import com.github.nuggets.model.vo.NuggetsAccountDetailVO;
import com.github.nuggets.repository.FansRepository;
import com.github.nuggets.repository.db.NuggetsFansMapper;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

/**
 * @description:
 * @author: wangsj
 * @date: 2022/3/17 6:40 下午
 */
@Service
public class FansRepositoryImpl extends ServiceImpl<NuggetsFansMapper, NuggetsFansEntity> implements FansRepository {


    @Override
    public Boolean addFansRecord(Long account, Long fans) {

        NuggetsFansEntity fansEntity = new NuggetsFansEntity();

        fansEntity.setAccountId(account);
        fansEntity.setFansId(fans);

        return this.save(fansEntity);
    }


    @Override
    public Optional<NuggetsFansEntity> findFansRecord(Long account, Long fans) {

        NuggetsFansEntity fansEntity = this.getOne(
                new LambdaQueryWrapper<NuggetsFansEntity>()
                        .eq(NuggetsFansEntity::getAccountId, account)
                        .eq(NuggetsFansEntity::getFansId, fans)
        );
        return Optional.ofNullable(fansEntity);
    }

    @Override
    public List<Long> listFansIds(Long account) {
        return this.list(
                new LambdaQueryWrapper<NuggetsFansEntity>()
                        .select(NuggetsFansEntity::getFansId)
                        .eq(NuggetsFansEntity::getAccountId, account)
        )
                .stream()
                .map(NuggetsFansEntity::getFansId)
                .collect(Collectors.toList());
    }

    @Override
    public PageResult<Long> pageFansIds(Long account, Integer pageIndex, Integer pageSize) {


        IPage<NuggetsFansEntity> page = page(
                //分页参数
                new Page<>(pageIndex, pageSize),
                //查询参数
                new LambdaQueryWrapper<NuggetsFansEntity>()
                        .select(NuggetsFansEntity::getFansId)
                        .eq(NuggetsFansEntity::getAccountId, account));

        List<Long> ids = page.getRecords().stream()
                .map(NuggetsFansEntity::getFansId)
                .collect(Collectors.toList());

        return PageResult.of(page, ids);

    }

}
